"""
This will be the default server configuration used for all tests in the parent directory
and any children directories. 
"""

import threading

from coffeetalk.protocol import ioml,iotypes
from coffeetalk.server import ionode, iotpd
from coffeetalk.utilities import authenticator,iologger

import default_tree

TEST_CONFIG = """
<ioml version="1.0">
<h />
<b>
<i n="Config" type="coffeetalk.server.startup.ServerConfig" version="1.5">
    <i n="StreamLogging" type="coffeetalk.server.settings.Logging">
        <i n="Name" type="[String]" value="TestServer" />
        <i n="LogLevel" type="[Integer]" value="0" />
        <i n="Error" type="[String]" value="None" />
        <i n="Access" type="[String]" value="None" />
    </i>
    <i n="FileLogging" type="coffeetalk.server.settings.Logging">
        <i n="Name" type="[String]" value="TestServer" />
        <i n="LogLevel" type="[Integer]" value="0" />
        <i n="Error" type="[String]" value="None" />
        <i n="Access" type="[String]" value="None" />
    </i>
    <i n="StandardLogging" type="coffeetalk.server.settings.Logging">
        <i n="Name" type="[String]" value="TestServer" />
        <i n="LogLevel" type="[Integer]" value="10" />
        <i n="Error" type="[String]" value="None" />
        <i n="Access" type="[String]" value="None" />
    </i>
    <i n="ClientAccess" type="coffeetalk.server.settings.Access">
        <i n="Allow" type="[List]" require="[IPv4]">
            <i n="Localhost" type="[IPv4]" value="127.0.0.1/255.255.255.255" />
        </i>
        <i n="Deny" type="[List]" require="[IPv4]" />
    </i>
    <i n="SystemAccess" type="coffeetalk.server.settings.Access">
        <i n="Allow" type="[List]" require="[IPv4]">
            <i n="Localhost" type="[IPv4]" value="127.0.0.1/255.255.255.255" />
        </i>
        <i n="Deny" type="[List]" require="[IPv4]"/>
    </i>
    <i n="Network" type="coffeetalk.server.settings.Network">
        <i n="UserPort" type="[Integer]" value="5050" />
        <i n="UserBind" type="[String]" value="127.0.0.1" />
        <i n="SystemPort" type="[Integer]" value="9091" />
        <i n="SystemBind" type="[String]" value="127.0.0.1" />
        <i n="SystemPassword" type="[String]" value="testing123" />
    </i>
</i>
</b>
</ioml>"""

class TestServer(iotpd.IOTPD):
    
    def __init__(self):
        self.authenticator = None
        iotpd.IOTPD.__init__(self)
    
    def start(self):
        self.serve_forever(configstring=TEST_CONFIG)
        self.init_auth()
        self.add_application(ioml.IOML(iomlstring=default_tree.TEST_TREE,nodetype=ionode.IONode).bodyelement)
           
    def init_auth(self):
        authconfig = iotypes.Connection(name='Auth')
        authconfig.username = 'TestUserAuthenticator'
        authconfig.host = '127.0.0.1'
        authconfig.password = 'testing123'
        authconfig.port = 9091
        authconfig.userclass = 'System'
        
        self.authenticator = Auth(authconfig,log=self.logger)
        self.authenticator.start()    

class Auth(authenticator.IOUserAuthenticator, threading.Thread):
    
    def __init__(self,ioclientconfig,log=iologger.IONullLogger()):
        authenticator.IOUserAuthenticator.__init__(self,ioclientconfig,log)
        threading.Thread.__init__(self)
        self.setName('User Authenticator')
        
    def run(self):
        self.connect()
    
    def join(self):
        self.disconnect()
            
    def handle_login(self,*args):
        credentials = args[2]
        if credentials.password == 'testing456':
            self.post_authorized(credentials)
        else:
            self.post_unauthorized(credentials)
             
if __name__ == '__main__':
    ts = TestServer()
    ts.start()
    